package com.ruoyi.certificate.service.impl;

import java.util.List;

import com.ruoyi.certificate.domain.UserRegisterRequest;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.certificate.mapper.NjtUserRegisterMapper;
import com.ruoyi.certificate.domain.NjtUserRegister;
import com.ruoyi.certificate.service.INjtUserRegisterService;

/**
 * 用户注册信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class NjtUserRegisterServiceImpl implements INjtUserRegisterService 
{
    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询用户注册信息
     * 
     * @param id 用户注册信息主键
     * @return 用户注册信息
     */
    @Override
    public NjtUserRegister selectNjtUserRegisterById(Long id)
    {
        return njtUserRegisterMapper.selectNjtUserRegisterById(id);
    }

    /**
     * 查询用户注册信息列表
     * 
     * @param njtUserRegister 用户注册信息
     * @return 用户注册信息
     */
    @Override
    public List<NjtUserRegister> selectNjtUserRegisterList(NjtUserRegister njtUserRegister)
    {
        return njtUserRegisterMapper.selectNjtUserRegisterList(njtUserRegister);
    }

    /**
     * 新增用户注册信息
     * 
     * @param njtUserRegister 用户注册信息
     * @return 结果
     */
    @Override
    public int insertNjtUserRegister(NjtUserRegister njtUserRegister)
    {
        njtUserRegister.setCreateTime(DateUtils.getNowDate());
        return njtUserRegisterMapper.insertNjtUserRegister(njtUserRegister);
    }

    /**
     * 修改用户注册信息
     * 
     * @param njtUserRegister 用户注册信息
     * @return 结果
     */
    @Override
    public int updateNjtUserRegister(NjtUserRegister njtUserRegister)
    {
        njtUserRegister.setUpdateTime(DateUtils.getNowDate());
        return njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
    }

    /**
     * 批量删除用户注册信息
     * 
     * @param ids 需要删除的用户注册信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserRegisterByIds(Long[] ids)
    {
        return njtUserRegisterMapper.deleteNjtUserRegisterByIds(ids);
    }

    /**
     * 删除用户注册信息信息
     * 
     * @param id 用户注册信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserRegisterById(Long id)
    {
        return njtUserRegisterMapper.deleteNjtUserRegisterById(id);
    }

    @Override
    public NjtUserRegister checkMobilePhone(String mobilePhone) {
        //去数据库查询手机号是否已经注册过

        return njtUserRegisterMapper.selectNjtUserRegisterByPhone(mobilePhone);
    }

    @Override
    public void register(UserRegisterRequest userRegisterRequest) {
        //保存到数据库中(系统用户表&用户注册表)
        //1.保存到系统用户表
        SysUser sysUser = new SysUser();
        sysUser.setUserName(userRegisterRequest.getMobilePhone());
        sysUser.setNickName(userRegisterRequest.getMobilePhone());
        sysUser.setPhonenumber(userRegisterRequest.getMobilePhone());
        sysUser.setPassword(userRegisterRequest.getPassword());
        sysUser.setCreateTime(DateUtils.getNowDate());
        sysUserMapper.insertUser(sysUser);
        //2.保存到用户注册表
        NjtUserRegister njtUserRegister = new NjtUserRegister();
        njtUserRegister.setId(sysUser.getUserId());
        njtUserRegister.setMobilePhone(userRegisterRequest.getMobilePhone());
        njtUserRegister.setCreateTime(sysUser.getCreateTime());
        njtUserRegister.setRegisterTime(sysUser.getCreateTime());
        njtUserRegister.setMemberFlag(0);
        njtUserRegister.setUserStatus(1);

        njtUserRegisterMapper.insertNjtUserRegister(njtUserRegister);
    }
}
